//
//  GRXMarketCell.m
//  1-MushroomStreet-自定义tabbar
//
//  Created by xmg on 15/12/25.
//  Copyright © 2015年 grx. All rights reserved.
//

#import "GRXMarketCell.h"
#import "GRXHeaderCollectionViewCell.h"
#import "GRXMarketsItem.h"
#import "GRXLoadDataTool.h"
#import <MJExtension/MJExtension.h>

@interface GRXMarketCell()<UICollectionViewDataSource,UICollectionViewDelegate>

/**标题label*/
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;

/**市场模型数组*/
@property (nonatomic,strong)NSArray *marketItemArray;


/**<#注释#>*/
@property (nonatomic, weak) UICollectionView *collectionView;


@end


@implementation GRXMarketCell

static NSString * const marketCell = @"market";

- (NSArray *)marketItemArray
{
    if (!_marketItemArray) {
        _marketItemArray = [[NSArray alloc] init];
    }
    return _marketItemArray;
}




- (void)awakeFromNib{

    
    [self setCollectionView];

    
    //加载数据
    [self loadData];
}



//数据加载
- (void)loadData{
    [GRXLoadDataTool loadData:^(NSDictionary *htmlData) {
        self.marketItemArray = [GRXMarketsItem mj_objectArrayWithKeyValuesArray:htmlData[@"markets"][@"list"]];
        
        self.titleLabel.text = htmlData[@"markets"][@"title"];
        
        //刷新数据
        [self.collectionView reloadData];
        
//        NSLog(@"%@",htmlData[@"markets"]);
    }];
}

- (void)setCollectionView{
    //创建流水布局
    CGFloat cellW = self.contentView.width / 4.0;
    CGFloat cellH = 124;
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    //设置流水布局属性
    //2.1 设置cell尺寸
    flowLayout.itemSize = CGSizeMake(cellW, cellH);
    
    //设置cell间距
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
    
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.titleLabel.frame), self.contentView.width, 124 * 2) collectionViewLayout:flowLayout];
    collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView = collectionView;
    //设置数据源
    collectionView.delegate = self;
    collectionView.dataSource = self;
    
    // 4.不允许collectionView滚动
    // 取消弹簧效果
    //        collectionView.bounces = NO;
    collectionView.scrollEnabled = NO;
    
    //注册cell
    [collectionView registerNib:[UINib nibWithNibName:@"GRXHeaderCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:marketCell];
    
    [self.contentView addSubview:collectionView];
    
    

}

#pragma mark ---UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.marketItemArray.count;
}



- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
    GRXHeaderCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:marketCell forIndexPath:indexPath];
    
    GRXMarketsItem *item = self.marketItemArray[indexPath.item];
    
    cell.marketItem = item;
    
    return cell;
}

#pragma mark ---UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    GRXMarketsItem *item = self.marketItemArray[indexPath.item];
    
    NSString *url = item.link;
    
    [[NSNotificationCenter defaultCenter] postNotificationName:GRXJumpDetailControllerNotification object:nil userInfo:@{@"url" : url}];
}
@end
